package me.firstapp.spi.shop.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.ListObject;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.shop.JsonCategory;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.shop.CategoryService;

@Service
public class CategoryServiceImpl extends BaseServiceImpl implements CategoryService {
	@Override
	public PageObject<JsonCategory> findCategoryPage(Integer pageNo, Integer pageSize, Map<String, Object> searchParams,
			String sortParam, String sortType) {
		PageObject<JsonCategory> result = new PageObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("sortParam", sortParam);
			parameters.put("sortType", sortType);
			JSONObject jsonObject = apiBody(parameters, "/category/findCategoryPage.do", "分页查询品类", searchParams);
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(jsonObject.getString("code"))) {
				JSONObject pageJsonObject = jsonObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonCategory> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonCategory> sectionList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonCategory category = JSONObject.toJavaObject(_jsonObject, JsonCategory.class);
								sectionList.add(category);
							}
						}
					}
					jsonPage.setList(sectionList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(jsonObject.getString("code"), jsonObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonCategory> findCategoryById(Long categoryId) {
		SingleObject<JsonCategory> result = new SingleObject<JsonCategory>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("categoryId", categoryId);

			JSONObject resultJson = api(parameters, "/category/findCategoryById.do", HttpMethod.GET, "通過id查詢品类");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonCategory apiJsonCategory = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonCategory.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonCategory);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonCategory> addCategory(String name, String description, Integer sort) {
		SingleObject<JsonCategory> result = new SingleObject<JsonCategory>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("name", name);
			parameters.put("description", description);
			parameters.put("sort", sort);

			JSONObject resultJson = api(parameters, "/category/addCategory.do", HttpMethod.POST, "新增品类");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonCategory apiJsonCategory = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonCategory.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonCategory);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<JsonCategory> updateCategory(Long categoryId, String name, String description, Integer sort) {
		SingleObject<JsonCategory> result = new SingleObject<JsonCategory>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("categoryId", categoryId);
			parameters.put("name", name);
			parameters.put("description", description);
			parameters.put("sort", sort);

			JSONObject resultJson = api(parameters, "/category/updateCategory.do", HttpMethod.POST, "修改品类");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JsonCategory apiJsonCategory = JSONObject.toJavaObject(resultJson.getJSONObject("object"),
						JsonCategory.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(apiJsonCategory);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> deleteCategorys(String ids) {
		SingleObject<Object> result = new SingleObject<>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("categoryIds", ids);

			JSONObject resultJson = api(parameters, "/category/deleteCategorys.do", HttpMethod.POST, "删除品类");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public ListObject<JsonCategory> findCategoryList() {
		ListObject<JsonCategory> result = new ListObject<>();
		try {
			JSONObject resultJson = api(null, "/category/findCategories.do", HttpMethod.GET, "查询商品品类");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				JSONArray jsonArray = resultJson.getJSONArray("items");
				List<JsonCategory> list = new ArrayList<>();
				if (jsonArray != null) {
					for (int i = 0; i < jsonArray.size(); i++) {
						JSONObject jsonObject = jsonArray.getJSONObject(i);
						if (jsonObject != null) {
							JsonCategory apiJsonCategory = JSONObject.toJavaObject(jsonObject, JsonCategory.class);
							list.add(apiJsonCategory);
						}
					}
				}
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setItems(list);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> updateCategoryCache() {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			JSONObject resultJson = api(null, "/category/updateCategoryCache.do", HttpMethod.POST, "更新商品种类缓存");
			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultJson.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultJson.getString("code"), resultJson.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}
}
